<?php

namespace StudyBuddy\Controllers;

use \StudyBuddy\WebPage;
use \StudyBuddy\Request;
//use \StudyBuddy\MongoDoc;

/**
 * Class for generating a popup page that starts the oauth dance
 * and this also serves as a callback url
 * to which paypal redirects after authorization
 *
 * Dependency is pecl OAuth extension!
 *
 */
class Tranferpaypal extends WebPage
{

	/**
	 *
	 * Viewer must have tranfer_paypal
	 * permission to access this page
	 * @var string
	 */
	protected $permission = 'tranfer_paypal';
        
	protected $oUserPaypal;
        
	/**
	 * Object of type Tranferpaypal
	 *
	 * We cannot just update the Viewer object because
	 * we need to create an object of type UserTwitter
	 * we will then replace the Viewer object with this new object
	 * via processLogin()
	 *
	 * @var object of type Tranferpaypal
	 */
	protected $oUser;

        
	/**
	 * The main purpose of this class is to
	 * generate the oAuth token
	 * and then redirect browser to paypal url with
	 * this unique token
	 *
	 * No actual page generation will take place
	 *
	 * @see classes/WebPage#main()
	 */
	protected function main(){
            $this->getUser();
//            debug($this->oRequest);
            
            if(!isset($this->oRequest['txn_id'])){
                $this->aPageVars['body'] = \tplTranferPayaplSuccess::parse(array('Error, please try again!', '', ''), false);
            } else {
                try {
					$coll = $this->oRegistry->Mongo->USER_PAYPAL;
					$coll->ensureIndex(array('i_uid' => 1));

					$aData = array('i_uid' => $this->oUser->getUid(), 'i_ts' => time(), 'date_tranfer' => date('r'), 'payer_email' => $this->oRequest['payer_email'], 'mc_gross' => $this->oRequest['mc_gross'], 'mc_currency' => $this->oRequest['mc_currency']);
								
								$this->oUserPaypal = \StudyBuddy\MongoDoc::factory($this->oRegistry, 'USER_PAYPAL', $aData);
					$res = $this->oUserPaypal->save($aData);
								d('$res: '.$res);
								
								$this->aPageVars['body'] = \tplTranferPayaplSuccess::parse(array('Tranfer paypal has been updated', '', ''), false);
				} catch (\Exception $e){
					e('Unable to save data to USER_PAYPAL collection because of '.$e->getMessage().' in file: '.$e->getFile().' on line: '.$e->getLine());
				}
            }
            
	}
        
        

	/**
	 * Create $this->oUser User object for user whose
	 * paypal is being tranfered
	 *
	 * @todo unfinished. IT will be possible to
	 * edit user other than Viewer when Viewer has
	 * permission to tranfer_other_paypal
	 * For now this is a Viewe object
	 *
	 * @return object $this
	 */
	protected function getUser(){
		$uid = $this->oRequest->get('uid', 'i', null);
		if($uid && ($uid !== $this->oRegistry->Viewer->getUid())){
			/**
			 * This is tranfer paypal for another user
			 * check Viewer permission here
			 */
			$this->checkAccessPermission('tranfer_any_paypal');
			$this->oUser = \StudyBuddy\User::factory($this->oRegistry)->by_id($uid);
		} else {
			$this->oUser = $this->oRegistry->Viewer;
		}

		return $this;
	}

}
